/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

Serum.Form.Module.Fields.EditPassword = Ext.extend(Ext.Panel, {

    /**
     * Constructor
     * 
     * @param   {Object}    baseConfig
     */
    constructor: function(baseConfig) {
        var config  = Ext.apply({
            formerpassword: {
                fieldLabel: __('base~form_label_formerpassword')
            },
            newpassword: {
                fieldLabel: __('base~form_label_newpassword')
            },
            repeatpassword: {
                fieldLabel: __('base~form_label_repeatpassword')
            },
            baseName: baseConfig.name || 'password'
        }, baseConfig);

        this.formerPasswordField = new Ext.form.TextField({
                                    fieldLabel: config.formerpassword.fieldLabel,
                                    inputType:  'password',
                                    name:       'custom-Base_Classes_Customs_EditPassword-old' + config.baseName
                                });

        this.newPasswordField = new Ext.form.TextField({
                                    fieldLabel: config.newpassword.fieldLabel,
                                    inputType:  'password',
                                    name:       'custom-Base_Classes_Customs_EditPassword-new' + config.baseName
                                });

        this.repeatPasswordField = new Serum.Form.Module.Fields.EditRepeatPasswordField(config, this);

        Serum.Form.Module.Fields.EditPassword.superclass.constructor.call(this, Ext.apply({
            border: false,
            layout: 'form',
            items: [
                this.formerPasswordField,
                this.newPasswordField,
                this.repeatPasswordField
            ]
        }, config));  
    },

    /**
     * Validate form
     *
     * @param   {String}    value
     */
    validateForm: function(value) {
        if (this.newPasswordField.getValue() !== value) {
            this.repeatPasswordField.markInvalid(__('base~form_errors_passwordnotequal'));
            return false;
        }

        this.repeatPasswordField.clearInvalid();
        return true;
    }
});

Serum.Form.Module.Fields.EditRepeatPasswordField = Ext.extend(Ext.form.TextField, {

    /**
     * Constructor
     *
     * @param   {Object}    config
     * @param   {Ext.Panel} panel
     */
    constructor: function(config, panel) {
        Serum.Form.Module.Fields.RepeatPasswordField.superclass.constructor.call(this, {
            fieldLabel: config.repeatpassword.fieldLabel,

            inputType:  'password',
            name:       'custom-Base_Classes_Customs_EditPassword-repeatnew' + config.baseName,

            validateValue: function(value) {
                if (Serum.Form.Module.Fields.RepeatPasswordField.superclass.validateValue.call(this, value) === true) {
                    return panel.validateForm(value);
                } else {
                    return false;
                }
            }
        });
    }
});

Ext.reg('editpasswordfield', Serum.Form.Module.Fields.EditPassword);